package com.tencent.cloud.iov.util.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.PreloadTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.tencent.cloud.iov.util.ThreadUtils;

import java.io.File;

/**
 * @author vinsonhuang
 * @date 2018/9/18
 */
public class ImageUtils {

    private static Context sContext;

    private ImageUtils() {

    }

    public static void init(@NonNull Context context) {
        sContext = context.getApplicationContext();
    }

    public static void bindImageView(ImageView imageView, String url) {
        bindImageView(imageView, url, null);
    }

    public static void bindImageView(ImageView imageView, String url, Drawable placeHolder) {
        GlideApp.with(sContext)
                .load(url)
                .placeholder(placeHolder)
                .into(imageView);
    }

    public static void downloadAndBindImage(final ImageView imageView, String url,
                                            Drawable placeHolder,
                                            final ImageListener listener) {
        GlideApp.with(sContext)
                .asFile()
                .load(url)
                .placeholder(placeHolder)
                .into(new ImageViewTarget<File>(imageView) {
                    @Override
                    protected void setResource(@Nullable File resource) {
                        if (null != resource) {
                            imageView.setImageBitmap(BitmapFactory.decodeFile(
                                    resource.getAbsolutePath()));
                            listener.onResourceReady(resource);
                        }
                    }
                });
    }

    public static void bindImageView(ImageView imageView, String url,
                                     Drawable placeHolder, int width, int height) {
        GlideApp.with(sContext)
                .load(url)
                .placeholder(placeHolder)
                .override(width, height)
                .into(imageView);
    }

    public static void bindImageViewUseCacheFile(ImageView imageView, String fileName,
                                                 Drawable placeHolder) {
        GlideApp.with(sContext)
                .load(new File(sContext.getExternalCacheDir(), fileName))
                .placeholder(placeHolder)
                .into(imageView);
    }

    public static void bindImageViewUseCacheFile(ImageView imageView, File file,
                                                 Drawable placeHolder) {
        GlideApp.with(sContext)
                .load(file)
                .placeholder(placeHolder)
                .into(imageView);
    }

    /**
     * 取消之前的加载操作
     *
     * @param view
     */
    public static void clear(View view) {
        GlideApp.with(sContext).clear(view);
    }

    public interface ImageListener {
        void onResourceReady(File resource);
    }
}
